package com.wyz.controller;

import com.wyz.mapper.OutStorageMapper;
import com.wyz.model.OutStorage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/7/27 21:08
 */
@Controller
public class OutStorageController {
    @Resource
    private OutStorageMapper outStorageMapper;
    @RequestMapping("/outStorage/getAllOutStorage")
    public String getAllOutStorage(HttpSession session) {
        List<Map<String, Object>> outStorage =outStorageMapper.selectOutStorageWithInventory();
        session.setAttribute("outStorage",outStorage);
        return "back/out_storage";
    }
    @RequestMapping("/outStorage/getOutStorageById")
    public String getOutStorageById(Integer id,HttpSession session) {
        List<Map<String, Object>> outStorage =outStorageMapper.selectOutStorageWithInventoryById(id);
        session.setAttribute("outStorage",outStorage);
        return "back/out_storageView";
    }
    @RequestMapping("/outStorage/addOutStorage")
    public String outStorageAdd() {
        return "back/out_storageAdd";
    }
    @RequestMapping("/outStorage/add.do")
    public String outStorageAdd(OutStorage outStorage, HttpSession session) {
        outStorageMapper.insert(outStorage);
        return "redirect:/outStorage/getAllOutStorage";
    }
    @RequestMapping("/outStorage/deleteOutStorage")
    public String outStorageDel(Integer id) {
        outStorageMapper.deleteById(id);
        return "redirect:/outStorage/getAllOutStorage";
    }
    @RequestMapping("/outStorage/edit.do")
    public String outStorageEdit(Integer id,HttpSession session) {
        OutStorage outStorage =outStorageMapper.selectById(id);
        session.setAttribute("outStorage",outStorage);
        return "back/out_storageEdit";
    }
    @RequestMapping("/outStorage/editHandler.do")
    public String outStorageEditHandler(OutStorage outStorage,HttpSession session) {
        int result =outStorageMapper.updateById(outStorage);
        if (result == 0) {
            session.setAttribute("error", "修改失败"); // 修改失败后将错误信息存入session中
            return "back/out_storageEdit";
        } else {
            session.removeAttribute("error"); // 修改成功后清除session中错误信息
            return "redirect:/outStorage/getAllOutStorage"; // 修改成功后重定向跳转到登录页面
        }

    }
    @RequestMapping("/outStorage/search.do")
    public String search(String condition,String sort,HttpSession session) {
        List<Map<String, Object>> outStorage =outStorageMapper.selectOutStorageWithInventoryLikeCondition(condition,sort);
        session.setAttribute("outStorage",outStorage);
        return "back/out_storage";
    }
}
